using System;
using Peepster.Core;

namespace Peepster.Infrastructure.Twitter
{
    public class YeddaTwitterService : ITwitterService
    {
        private Yedda.Twitter _service;

        public YeddaTwitterService()
        {
            _service = new Yedda.Twitter();
            _service.TwitterClient = AppSettings.ClientName;
            _service.TwitterClientUrl = AppSettings.ClientUrl;
        }

        public void SendTweet(string message)
        {
            try
            {
                System.Net.ServicePointManager.Expect100Continue = false;
                _service.UpdateAsJSON(AppSettings.TwitterUserName, AppSettings.TwitterPassword, message);
            }
            catch (Exception e)
            {
                throw new ApplicationException("Unable to send tweet\n" + e.Message);
            }
        }

        public bool ValidateAccount(string userName, string password)
        {
            try
            {
                _service.GetUserTimelineAsJSON(AppSettings.TwitterUserName, AppSettings.TwitterPassword);
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}